# R code for chapter 52 Multiway tables and crosstabs # Book details: # Health Science Statistics using R and R Commander # by Robin Beaumont # Paperback - ISBN 9781907904318 # http://www.amazon.co.uk/Health-Science-Statistics-using-Commander/dp/190790431X # or direct from publisher at: # http://www.scionpublishing.com/ # book website: http://www.robin-beaumont.co.uk/rbook ########################################################### # mydataframe <-NULL mydataframe <- read.table("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/crosstabs1.dat",header=T) str(mydataframe) ## mean values of each result: tapply(mydataframe$result, mydataframe [c('sex','illness')],mean) ## by sex of patient and type of interviewer tapply(mydataframe$result, mydataframe [c('sex','illness','grouping')],mean) # now for the counts instead tapply(mydataframe$result, mydataframe [c('sex','illness','grouping')], length ) # section 52.1 SPSS style crosstabs install.packages("gmodels", dependencies=TRUE) library(gmodels) CrossTable(mydataframe$illness, mydataframe$grouping, format="SPSS") # section 52.2 Mosaic plots for multiway tables install.packages("vcd", dependencies=TRUE) library(vcd) mosaic(~ grouping +illness + sex, data = mydataframe, main = "Proportions by group for illness & sex", shade = TRUE, legend = TRUE) mosaic(~ sex +illness, data = mydataframe, main = "Proportions by group for illness & sex", shade = TRUE, legend = TRUE, residuals_type = "pearson", gp = shading_Friendly)